//
// Created by Yang on 2023/5/28.
//
#include "common.h"
#include "account_item.h"

void loadDataFromFile(vector<AccountItem>& items){
    // ios::out | ios::app | ios::ate| ios::in| ios::binary
    ifstream input(FILENAME);

    AccountItem item;
    string line;
    while (input >> item.itemType >> item.amount >> item.detail) {
        items.push_back(item);
    }

    input.close();
}

// ------------------------------------ 1. 记账 ----------------------------------------
void accounting(vector<AccountItem>& items){
    // 3. 读取键盘选择， 并作合法性校验
    char key = readMenuSelection(3);

    switch (key) {
        case '1': // 1 - 收入
            income(items);
            break;
        case '2': // 2 - 支出
            expand(items);
            break;
        case '3': // 3 - 退出
            break;
        default:
            break;
    }
}

void income(vector<AccountItem>& items){
    AccountItem item;

    item.itemType = INCOME;

    // 获取用户输入数据的金额和备注信息
    cout << "\n请输入金额:" << endl;
    item.amount = readAmount();

    // 备注信息
    cout << "\n请输入备注信息:" << endl;
    getline(cin, item.detail);

    items.push_back(item);

    insertIntoFile(item);

    cout << "\n========================== 记账成功 =====================" << endl;
    cout << "\n请按回车键返回主菜单..." << endl;

    string line;
    getline(cin, line);
}

void insertIntoFile(const AccountItem item) {
    ofstream output(FILENAME, ios::out | ios::app);
    output << item.itemType << "\t" << item.amount << "\t" << item.detail << endl;
    output.close();
}

void expand(vector<AccountItem> &items) {
    AccountItem item;

    item.itemType = EXPAND;

    // 获取用户输入数据的金额和备注信息
    cout << "\n请输入金额:" << endl;
    item.amount = -readAmount();

    // 备注信息
    cout << "\n请输入备注信息:" << endl;
    getline(cin, item.detail);

    items.push_back(item);

    insertIntoFile(item);

    cout << "\n========================== 记账成功 =====================" << endl;
    cout << "\n请按回车键返回主菜单..." << endl;

    string line;
    getline(cin, line);
}

// ------------------------------------ 2. 查询 ----------------------------------------
void query(const vector<AccountItem>& items){
    // 3. 读取键盘选择， 并作合法性校验
    char key = readMenuSelection(4);

    switch (key) {
        case '1': // 1 - 全部
            queryItems(items);
            break;
        case '2': // 2 - 收入
            queryItems(items, INCOME);
            break;
        case '3': // 3 - 支出
            queryItems(items, EXPAND);
            break;
        default:
            break;
    }
}

void queryItems(const vector<struct AccountItem>& items, const string typ) {
    cout << "-------------------------- 查询结果 ------------------------" << endl;
    cout << "类型\t\t金额\t\t备注" << endl;
    int toltal = 0;
    for (auto item: items) {
        if (item.itemType == typ) {
            printItem(item);
            toltal += item.amount;
        }
    }
    cout << "-------------------------- 查询成功 ------------------------" << endl;
    cout << "总计:" << toltal << endl;
    cout << "\n请按回车键返回主菜单..." << endl;

    string line;
    getline(cin, line);
}

void printItem(const AccountItem& item) {
    cout << item.itemType << "\t" << item.amount << "\t" << item.detail << endl;
}

void queryItems(const vector<AccountItem>& items) {
    cout << "-------------------------- 查询结果 ------------------------" << endl;
    cout << "类型\t\t金额\t\t备注" << endl;
    int toltal = 0;
    for (auto item: items) {
        printItem(item);
        toltal += item.amount;
    }
    cout << "-------------------------- 查询成功 ------------------------" << endl;
    cout << "总计:" << toltal << endl;
    cout << "\n请按回车键返回主菜单..." << endl;

    string line;
    getline(cin, line);
}


